namespace ClassyBlog.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;

    using ExtensionMethods;

    public class ThemeableRazorViewEngine : ThemeableBuildManagerViewEngine
    {
        public ThemeableRazorViewEngine(
            IBuildManager buildManager,
            IViewPageActivator viewPageActivator) :
            base(buildManager, viewPageActivator)
        {
            AreaViewLocationFormats.AddAll(new[]
               {
                   "~/Areas/{2}/Views/{3}/{1}/{0}.cshtml",
                   "~/Areas/{2}/Views/{3}/{1}/{0}.vbhtml",
                   "~/Areas/{2}/Views/{3}/Shared/{0}.cshtml",
                   "~/Areas/{2}/Views/{3}/Shared/{0}.vbhtml",
                   "~/Areas/{2}/Views/{1}/{0}.cshtml",
                   "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                   "~/Areas/{2}/Views/Shared/{0}.cshtml",
                   "~/Areas/{2}/Views/Shared/{0}.vbhtml"
               });

            AreaMasterLocationFormats.AddAll(new[]
             {
                 "~/Areas/{2}/Views/{3}/{1}/{0}.cshtml",
                 "~/Areas/{2}/Views/{3}/{1}/{0}.vbhtml",
                 "~/Areas/{2}/Views/{3}/Shared/{0}.cshtml",
                 "~/Areas/{2}/Views/{3}/Shared/{0}.vbhtml",
                 "~/Areas/{2}/Views/{1}/{0}.cshtml",
                 "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                 "~/Areas/{2}/Views/Shared/{0}.cshtml",
                 "~/Areas/{2}/Views/Shared/{0}.vbhtml"
             });

            AreaPartialViewLocationFormats.AddAll(new[]
              {
                  "~/Areas/{2}/Views/{3}/{1}/{0}.cshtml",
                  "~/Areas/{2}/Views/{3}/{1}/{0}.vbhtml",
                  "~/Areas/{2}/Views/{3}/Shared/{0}.cshtml",
                  "~/Areas/{2}/Views/{3}/Shared/{0}.vbhtml",
                  "~/Areas/{2}/Views/{1}/{0}.cshtml",
                  "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                  "~/Areas/{2}/Views/Shared/{0}.cshtml",
                  "~/Areas/{2}/Views/Shared/{0}.vbhtml"
              });

            ViewLocationFormats.AddAll(new[]
               {
                   "~/Views/{2}/{1}/{0}.cshtml",
                   "~/Views/{2}/{1}/{0}.vbhtml",
                   "~/Views/{2}/Shared/{0}.cshtml",
                   "~/Views/{2}/Shared/{0}.vbhtml",
                   "~/Views/{1}/{0}.cshtml",
                   "~/Views/{1}/{0}.vbhtml",
                   "~/Views/Shared/{0}.cshtml",
                   "~/Views/Shared/{0}.vbhtml"
               });

            MasterLocationFormats.AddAll(new[]
                 {
                     "~/Views/{2}/{1}/{0}.cshtml",
                     "~/Views/{2}/{1}/{0}.vbhtml",
                     "~/Views/{2}/Shared/{0}.cshtml",
                     "~/Views/{2}/Shared/{0}.vbhtml",
                     "~/Views/{1}/{0}.cshtml",
                     "~/Views/{1}/{0}.vbhtml",
                     "~/Views/Shared/{0}.cshtml",
                     "~/Views/Shared/{0}.vbhtml"
                 });

            PartialViewLocationFormats.AddAll(new[]
                {
                   "~/Views/{2}/{1}/{0}.cshtml",
                   "~/Views/{2}/{1}/{0}.vbhtml",
                   "~/Views/{2}/Shared/{0}.cshtml",
                   "~/Views/{2}/Shared/{0}.vbhtml",
                   "~/Views/{1}/{0}.cshtml",
                   "~/Views/{1}/{0}.vbhtml",
                   "~/Views/Shared/{0}.cshtml",
                   "~/Views/Shared/{0}.vbhtml"
               });

            ViewStartFileExtensions = new List<string>(new[]
                                                           {
                                                               "cshtml",
                                                               "vbhtml"
                                                           });
        }

        protected ICollection<string> ViewStartFileExtensions
        {
            get; private set;
        }

        protected override IView CreatePartialView(
            ControllerContext controllerContext,
            string partialPath)
        {
            return new RazorView(
                controllerContext,
                partialPath,
                null,
                false,
                ViewStartFileExtensions,
                ViewPageActivator);
        }

        protected override IView CreateView(
            ControllerContext controllerContext,
            string viewPath,
            string masterPath)
        {
            return new RazorView(
                controllerContext,
                viewPath,
                masterPath,
                true,
                ViewStartFileExtensions,
                ViewPageActivator);
        }

        protected override bool IsValidCompiledType(
            ControllerContext controllerContext,
            string virtualPath,
            Type compiledType)
        {
            return typeof(WebViewPage).IsAssignableFrom(compiledType);
        }
    }
}